package com.zjxny;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.map.MapBuilder;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;

/**
 * 违章页面
 * https://gd.122.gov.cn/views/memfyy/vehinfo.html?index=7
 * 
 * 
 * @author simple
 *
 */
public class TrafficExample {

	public static void main(String[] args) throws IOException {
		String url = "https://gd.122.gov.cn/user/m/uservio/suriquery";
		//startDate=20230101&endDate=20230913&hpzl=52&hphm=%E7%B2%A4BBA7709&page=1&type=0
		
//		Map<String,Object> params = MapUtil.createMap(new Map<String,Object>)
		MapBuilder<String,Object> builder=MapUtil.builder(new HashMap<String, Object>())
				.put("startDate", "20230101")
				.put("endDate", "20231231")
				.put("hpzl", 52)
				.put("hphm","粤BBR0733")
				.put("page", 1)
				.put("type", 0);
		String referer = "https://gd.122.gov.cn/views/memfyy/violation.html";
		HttpRequest request = HttpRequest.post(url).header("Referer",referer);
		request.header("User-Agent","Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36");
		
		String cookieStr = FileUtil.readString(new File("./data/cookie.json"), Charset.forName("utf-8"));
		JSONArray arr = JSONUtil.parseArray(cookieStr);
		
		List<String> cookies = arr.stream().map(s-> {
			JSON t = (JSON) s;
			return String.format("%s=%s",t.getByPath("name",String.class),t.getByPath("value",String.class) );
		}).collect(Collectors.toList());
		
//		request.header("Cookie", "_uab_collina=169461749607378790055193; c_yhlx_=2; JSESSIONID-L=c6973208-0a51-45f5-ac4a-dd034171edc0; accessToken=gxRec6E7Bg7xLRWtNMX3VIirKU+UsWk04wQzmKGQi9oniFvjjzwEyPXC8OsOBIvHPexZgb9VNdsEPzct9l0hqVtRkam8Y0zmXYSbDL9SMmNkRpZ/kWAD6aAV0IUDeHVFJPTbuthtblY/R4gWe2174PN014ifAcb4/nA1gkccQfhORceExfXwoJVXfzfk7ZXp; JSESSIONID=AE07286DACB9B24BC58B8F94A3FEE7CE; tmri_csfr_token=EE088AC2B9D9756A913470FE2FDF28C4");
		
		request.header("Cookie",StrUtil.join("; ", cookies));
		
		HttpResponse response = request.form(builder.build()).execute();
		System.err.println(response.getStatus());
		
		String responseStr = response.body();
		if(response.getStatus()==200) {
			System.err.println(response);
		} else {
			//
		}
	}
}